<template>
  <div class="app-container">
    <div class="filter-container">
      <el-card>
        <el-form ref="searchform" :inline="true" :model="searchform" class="demo-form-inline" size="small">
          <el-form-item label="用户信息：">
            <el-input v-model="searchform.Keywords" clearable placeholder="账号/昵称/电话" />
          </el-form-item>
          <el-form-item label="注册日期：">
            <el-date-picker
              v-model="searchform.CreateTime"
              type="daterange"
              align="right"
              value-format="yyyy-MM-dd"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch()">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <div class="list-btn-container">
        <el-button-group>
          <slot v-for="itemf in loadBtnFunc">
            <el-button v-if="itemf.FullName==='新增'" type="default" icon="el-icon-plus" size="small" @click="ShowEditOrViewDialog()">新增</el-button>
            <el-button v-if="itemf.FullName==='修改'" type="default" icon="el-icon-edit" size="small" @click="ShowEditOrViewDialog('edit')">修改</el-button>
            <el-button v-if="itemf.FullName=='禁用'" type="default" icon="el-icon-video-pause" size="small" @click="setEnable('0')">禁用</el-button>
            <el-button v-if="itemf.FullName=='启用'" type="default" icon="el-icon-video-play" size="small" @click="setEnable('1')">启用</el-button>
            <el-button v-if="itemf.FullName==='软删除'" type="default" icon="el-icon-delete" size="small" @click="deleteSoft('0')">软删除</el-button>
            <el-button v-if="itemf.FullName==='删除'" type="default" icon="el-icon-delete" size="small" @click="deletePhysics()">删除</el-button>
            <el-button v-if="itemf.FullName==='重置密码'" type="default" icon="el-icon-refresh-right" size="small" @click="handleResetPassword()">重置密码</el-button>
          </slot>
          <el-button type="default" icon="el-icon-refresh" size="small" @click="loadTableData()">刷新</el-button>
        </el-button-group>
      </div>
      <el-table
        ref="gridtable"
        v-loading="tableloading"
        :data="tableData"
        border
        stripe
        highlight-current-row
        style="width: 100%"
        :default-sort="{prop: 'SortCode', order: 'descending'}"
        @select="handleSelectChange"
        @select-all="handleSelectAllChange"
        @sort-change="handleSortChange"
      >
        <el-table-column
          type="selection"
          width="30"
        />
        <el-table-column
          prop="Id"
          label="编号"
          sortable="custom"
          width="100"
        />
        <el-table-column
          prop="NickName"
          label="昵称"
          sortable="custom"
          width="100"
          align="center"
        />
        <el-table-column
          prop="RealName"
          label="姓名"
          sortable="custom"
          width="100"
          align="center"
        />
        <el-table-column
          prop="Gender"
          label="性别"
          sortable="custom"
          width="90"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.Gender=== 1 ? '男' : '女' }}
          </template>
        </el-table-column>
        <!-- <el-table-column
          prop="Birthday"
          label="生日"
          sortable="custom"
          width="120"
          align="center"
          :formatter="dateformatter"
        /> -->
        <el-table-column
          prop="MobilePhone"
          label="手机号码"
          sortable="custom"
          width="120"
          align="center"
        />
        <el-table-column
          prop="BuyGiftCount"
          label="礼盒购买次数"
          sortable="custom"
          width="150"
          align="center"
        />
        <el-table-column
          prop="MemberGradeName"
          label="等级"
          sortable="custom"
          width="100"
          align="center"
        />
        <el-table-column
          prop="ReferralUserName"
          label="上级推广员"
          sortable="custom"
          width="120"
          align="center"
        />
        <el-table-column
          label="可用"
          sortable="custom"
          width="90"
          prop="EnabledMark"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.EnabledMark === true ? 'success' : 'info'"
              disable-transitions
            >{{ scope.row.EnabledMark===true?'启用':'禁用' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="CreatorTime"
          label="添加时间"
          width="180"
          sortable
        />
        <el-table-column
          fixed="right"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="ShowViewDialog(scope.row)">查看</el-button>
            <el-button type="text" size="small" @click="ShowLevelEditOrViewDialog('select',scope.row)">修改等级</el-button>
            <el-button type="text" size="small" @click="ShowReferralEditOrViewDialog('select',scope.row)">修改上级</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="pagination.currentPage"
          :page-sizes="[5,10,20,50,100, 200, 300, 400]"
          :page-size="pagination.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.pageTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog ref="dialogEditForm" :title="editFormTitle+'用户'" :visible.sync="dialogEditFormVisible" width="660px">
      <el-form ref="editFrom" :inline="true" :model="editFrom" :rules="rules" class="demo-form-inline">

        <el-form-item label="编号" :label-width="formLabelWidth" prop="Id">
          <el-input v-model="editFrom.Id" placeholder="请输入账号" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="昵称" :label-width="formLabelWidth" prop="NickName">
          <el-input v-model="editFrom.NickName" placeholder="请输入昵称" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="RealName">
          <el-input v-model="editFrom.RealName" placeholder="请输入姓名" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="返利点数" :label-width="formLabelWidth" prop="NoRebateTimes">
          <el-input-number v-model="editFrom.NoRebateTimes" min="0" placeholder="请输入返利点数" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth" prop="MobilePhone">
          <el-input v-model="editFrom.MobilePhone" placeholder="请输入手机号" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="Gender">
          <el-radio-group v-model="editFrom.Gender" :disabled="IsDisabled">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="微信号" :label-width="formLabelWidth" prop="WeChat">
          <el-input v-model="editFrom.WeChat" placeholder="请输入微信号" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="Email">
          <el-input v-model="editFrom.Email" placeholder="请输入Email @" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
        <el-form-item label="出身日期" :label-width="formLabelWidth" prop="Birthday">
          <el-date-picker
            v-model="editFrom.Birthday"
            type="date"
            placeholder="选择日期"
            :disabled="IsDisabled"
          />
        </el-form-item>
        <el-form-item label="选项" :label-width="formLabelWidth" prop="">
          <el-checkbox v-model="editFrom.EnabledMark">启用</el-checkbox>
          <!-- <el-checkbox v-model="editFrom.IsAdministrator">管理员</el-checkbox> -->
        </el-form-item>

        <!-- <el-form-item label="所属组织" :label-width="formLabelWidth" prop="OrganizeId">
          <el-cascader
            v-model="selectedOrganizeOptions"
            style="width:500px;"
            :options="selectOrganize"
            filterable
            :props="{label:'FullName',value:'Id',children:'Children',emitPath:false, checkStrictly: true,expandTrigger: 'hover' }"
            clearable
            @change="handleSelectOrganizeChange"
          />
        </el-form-item> -->
        <!-- <el-form-item label="岗位角色" :label-width="formLabelWidth" prop="RoleId">
          <el-select
            v-model="editFrom.RoleId"
            style="width:500px"
            multiple
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in selectRole"
              :key="item.Id"
              :label="item.FullName"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>-->
        <br>
        <el-form-item label="头像    " :label-width="formLabelWidth" prop="HeadIcon">
          <ele-upload-image
            v-model="editFrom.HeadIcon"
            :file-size="5"
            :is-show-tip="false"
            :lazy="true"
            :response-fn="handleResponse"
            action=""
            :disabled="IsDisabled"
            style=" margin-right:50px;"
          />
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="Description">
          <el-input v-model="editFrom.Description" style="width:500px" placeholder="" autocomplete="off" clearable :disabled="IsDisabled" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEditForm()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog ref="dialogEditForm" :title="editFormTitle" :visible.sync="dialogLevleEditFormVisible" width="660px">
      <el-form ref="editUserLevelFrom" :inline="true" :model="editFrom" :rules="rules" class="demo-form-inline">
        <el-form-item label="等级" :label-width="formLabelWidth" prop="MemberGradeId">
          <el-select v-model="editUserLevelFrom.MemberGradeId" clearable placeholder="请选择等级" style="width:500px;">
            <el-option
              v-for="item in selectLeves"
              :key="item.Id"
              :label="item.Name"
              :value="item.Id"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="上级" :label-width="formLabelWidth" prop="ParentUserId">
          <el-select
            v-model="editFrom.ParentUserId"
            style="width:500px"
            placeholder="请选择上级"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogLevleEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="SaveLevelEditForm()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog ref="dialogEditForm" :title="editFormTitle" :visible.sync="dialogReferralEditFormVisible" width="660px">
      <el-form ref="editUserReferralFrom" :inline="true" :model="editFrom" :rules="rules" class="demo-form-inline">
        <el-form-item label="上级" :label-width="formLabelWidth" prop="ReferralUserId">
          <el-select
            v-model="editUserReferralFrom.ReferralUserId"
            style="width:500px"
            filterable
            placeholder="请选择上级"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogReferralEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="SaveReferralEditForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { getUserListWithPager, getUserDetail, saveUser, setUserEnable,
  deleteSoftUser, deleteUser, resetPassword, GetAllUsersEnable, UpdateLevel, UpdateReferral } from '@/api/security/userservice'
import * as fecha from 'element-ui/lib/utils/date'
import { getAllRoleList } from '@/api/security/roleservice'
import { getAllOrganizeTreeTable } from '@/api/security/organizeservice'
import { GetAllEnable } from '@/api/shop/membergrade'
import EleUploadImage from '@/components/upload/EleUploadImage'
export default {
  components: {
    EleUploadImage
  },
  data() {
    return {
      searchform: {
        RoleId: '',
        Keywords: '',
        CreateTime: ''
      },
      IsDisabled: false,
      selectRole: [],
      selectedOrganizeOptions: '',
      selectOrganize: [],
      dialogEditFormVisible: false,
      dialogLevleEditFormVisible: false,
      dialogReferralEditFormVisible: false,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime())
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 1)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近两天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近两个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 60)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      loadBtnFunc: [],
      selectLeves: [],
      tableData: [],
      options: [],
      list: [],
      tableloading: true,
      pagination: {
        currentPage: 1,
        pagesize: 10,
        pageTotal: 0
      },
      sortableData: {
        order: '',
        sort: ''
      },
      editFormTitle: '',
      editFrom: {
        Id: '',
        Account: '',
        RealName: '',
        NickName: '',
        Gender: '1',
        Birthday: '',
        MobilePhone: '',
        Email: '',
        WeChat: '',
        OrganizeId: '',
        RoleId: '',
        IsAdministrator: false,
        EnabledMark: true,
        Description: '',
        ReferralUserName: '',
        MemberGradeName: '',
        NoRebateTimes: 0
      },
      editUserLevelFrom: {
        MemberGradeId: '',
        Id: ''
      },
      editUserReferralFrom: {
        ReferralUserId: '',
        Id: ''
      },
      rules: {
        Account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 16 个字符', trigger: 'blur' }
        ],
        RealName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        OrganizeId: [
          { required: true, message: '请输选择所属组织', trigger: 'blur' }
        ],
        RoleId: [
          { required: true, message: '请输选择岗位角色', trigger: 'blur' }
        ]
      },
      formLabelWidth: '80px',
      currentId: '', // 当前操作对象的ID值，主要用于修改
      currentSelected: []
    }
  },
  created() {
    this.pagination.currentPage = 1
    this.InitDictItem()
    this.loadTableData()
    this.loadBtnFunc = JSON.parse(localStorage.getItem('yueboncurrentfuns'))
  },
  methods: {
    /**
     * 初始化数据
     */
    InitDictItem() {
      getAllRoleList().then(res => {
        this.selectRole = res.ResData
      })
      getAllOrganizeTreeTable().then(res => {
        this.selectOrganize = res.ResData
      })
      GetAllEnable().then(res => {
        this.selectLeves.push({
          'Id': '',
          'Name': '普通用户',
          'Level': 0,
          'FirstLevelReturnRatio': 0,
          'TwoLevelReturnRatio': 0,
          'CreateTime': '2020-08-11 17:23:39',
          'CreateId': '1',
          'UpdateId': '1',
          'DeleteMark': 0,
          'MeetPrice': 0,
          'TeamCommissionRate': 0,
          'Price': 0
        })
        res.ResData.forEach(s => {
          this.selectLeves.push(s)
        })
      })
      GetAllUsersEnable().then(res => {
        this.options = res.ResData.map(item => {
          return { value: `${item.Id}`, label: `${item.NickName}_${item.Id}` }
        })
      })
    },
    /**
     * 加载页面table数据
     */
    loadTableData: function() {
      this.tableloading = true
      var seachdata = {
        'CurrentPage': this.pagination.currentPage,
        'length': this.pagination.pagesize,
        'Keywords': this.searchform.Keywords,
        'CreatorTime1': this.searchform.CreateTime[0],
        'CreatorTime2': this.searchform.CreateTime[1],
        'RoleId': this.searchform.RoleId,
        'Order': this.sortableData.order,
        'Sort': this.sortableData.sort
      }
      getUserListWithPager(seachdata).then(res => {
        this.tableData = res.ResData.Items
        this.pagination.pageTotal = res.ResData.TotalItems
        this.tableloading = false
      })
    },
    /**
     * 点击查询
     */
    handleSearch: function() {
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    ShowViewDialog: function(item) {
      this.currentId = item.Id
      this.editFormTitle = '查看'
      this.dialogEditFormVisible = true
      this.bindEditInfo()
    },
    handleResponse(response, file, fileList) {
      this.editFrom.HeadIcon = response.ResData.FilePath
      console.log(this.editFrom.HeadIcon)
      return this.editFrom.HeadIcon
    },
    ShowLevelEditOrViewDialog: function(view, row) {
      this.currentId = row.Id
      this.editFormTitle = '修改等级'
      this.dialogLevleEditFormVisible = true
      this.editUserLevelFrom.MemberGradeId = row.MemberGradeId
    },
    ShowReferralEditOrViewDialog: function(view, row) {
      this.currentId = row.Id
      this.editFormTitle = '修改上级'
      this.dialogReferralEditFormVisible = true
      this.editUserLevelFrom.ReferralUserId = row.ReferralUserId
    },
    /**
     * 新增、修改或查看明细信息（绑定显示数据）     *
     */
    ShowEditOrViewDialog: function(view) {
      this.IsDisabled = false
      if (view !== undefined) {
        if (this.currentSelected.length > 1 || this.currentSelected.length === 0) {
          this.$alert('请选择一条数据进行编辑/修改', '提示')
        } else {
          this.currentId = this.currentSelected[0].Id
          this.editFormTitle = '编辑'
          this.dialogEditFormVisible = true
          this.bindEditInfo()
        }
      } else {
        this.editFormTitle = '新增'
        this.currentId = ''
        this.selectedOrganizeOptions = ''
        this.dialogEditFormVisible = true
        this.$refs['editFrom'].resetFields()
      }
    },
    bindEditInfo: function() {
      getUserDetail(this.currentId).then(res => {
        this.editFrom.Id = res.ResData.Id
        this.editFrom.Account = res.ResData.Account
        this.editFrom.RealName = res.ResData.RealName
        this.editFrom.NickName = res.ResData.NickName
        this.editFrom.Gender = res.ResData.Gender + ''
        this.editFrom.Birthday = res.ResData.Birthday
        this.editFrom.MobilePhone = res.ResData.MobilePhone
        this.editFrom.Email = res.ResData.Email
        this.editFrom.WeChat = res.ResData.WeChat
        this.editFrom.OrganizeId = res.ResData.OrganizeId
        this.editFrom.IsAdministrator = res.ResData.IsAdministrator
        this.editFrom.HeadIcon = res.ResData.HeadIcon
        this.editFrom.EnabledMark = res.ResData.EnabledMark
        this.editFrom.RoleId = res.ResData.RoleId.split(',')
        this.editFrom.Description = res.ResData.Description
        this.selectedOrganizeOptions = res.ResData.OrganizeId
        this.MemberGradeName = res.ResData.MemberGradeName
        this.ReferralUserName = res.ResData.ReferralUserName
        this.editFrom.NoRebateTimes = res.ResData.NoRebateTimes
      })
    },
    /**
     * 新增/修改保存
     */
    saveEditForm() {
      console.log(this.editFrom.RoleId)
      this.$refs['editFrom'].validate((valid) => {
        if (valid) {
          const data = {
            Account: this.editFrom.Account,
            RealName: this.editFrom.RealName,
            NickName: this.editFrom.NickName,
            Gender: this.editFrom.Gender,
            Birthday: this.editFrom.Birthday,
            MobilePhone: this.editFrom.MobilePhone,
            Email: this.editFrom.Email,
            WeChat: this.editFrom.WeChat,
            OrganizeId: this.editFrom.OrganizeId,
            IsAdministrator: this.editFrom.IsAdministrator,
            EnabledMark: this.editFrom.EnabledMark,
            RoleId: this.editFrom.RoleId.join(','),
            Description: this.editFrom.Description,
            HeadIcon: this.editFrom.HeadIcon,
            NoRebateTimes: this.editFrom.NoRebateTimes,
            'Id': this.currentId
          }

          var url = 'User/Insert'
          if (this.currentId !== '') {
            url = 'User/Update?id=' + this.currentId
          }
          saveUser(data, url).then(res => {
            if (res.Success) {
              this.$message({
                message: '恭喜你，操作成功',
                type: 'success'
              })
              this.dialogEditFormVisible = false
              this.currentSelected = ''
              this.selectedOrganizeOptions = ''
              this.$refs['editFrom'].resetFields()
              this.loadTableData()
              this.InitDictItem()
            } else {
              this.$message({
                message: res.ErrMsg,
                type: 'error'
              })
            }
          })
        } else {
          return false
        }
      })
    },

    setEnable: function(val) {
      if (this.currentSelected.length === 0) {
        this.$alert('请先选择要操作的数据', '提示')
        return false
      } else {
        var currentIds = ''
        this.currentSelected.forEach(element => {
          currentIds += element.Id + ','
        })
        const data = {
          ids: currentIds,
          bltag: val
        }
        setUserEnable(data).then(res => {
          if (res.Success) {
            this.$message({
              message: '恭喜你，操作成功',
              type: 'success'
            })
            this.currentSelected = ''
            this.loadTableData()
          } else {
            this.$message({
              message: res.ErrMsg,
              type: 'error'
            })
          }
        })
      }
    },
    deleteSoft: function(val) {
      if (this.currentSelected.length === 0) {
        this.$alert('请先选择要操作的数据', '提示')
        return false
      } else {
        var currentIds = ''
        this.currentSelected.forEach(element => {
          currentIds += element.Id + ','
        })
        const data = {
          ids: currentIds,
          bltag: val
        }
        deleteSoftUser(data).then(res => {
          if (res.Success) {
            this.$message({
              message: '恭喜你，操作成功',
              type: 'success'
            })
            this.currentSelected = ''
            this.loadTableData()
          } else {
            this.$message({
              message: res.ErrMsg,
              type: 'error'
            })
          }
        })
      }
    },
    deletePhysics: function() {
      if (this.currentSelected.length === 0) {
        this.$alert('请先选择要操作的数据', '提示')
        return false
      } else {
        var currentIds = ''
        this.currentSelected.forEach(element => {
          currentIds += element.Id + ','
        })
        const data = {
          ids: currentIds
        }
        deleteUser(data).then(res => {
          if (res.Success) {
            this.$message({
              message: '恭喜你，操作成功',
              type: 'success'
            })
            this.currentSelected = ''
            this.loadTableData()
          } else {
            this.$message({
              message: res.ErrMsg,
              type: 'error'
            })
          }
        })
      }
    },
    /**
     * 当表格的排序条件发生变化的时候会触发该事件
     */
    handleSortChange: function(column) {
      this.sortableData.sort = column.prop
      if (column.order === 'ascending') {
        this.sortableData.order = 'asc'
      } else {
        this.sortableData.order = 'desc'
      }
      this.loadTableData()
    },
    /**
     * 当用户手动勾选checkbox数据行事件
     */
    handleSelectChange: function(selection, row) {
      this.currentSelected = selection
    },
    /**
     * 当用户手动勾选全选checkbox事件
     */
    handleSelectAllChange: function(selection) {
      this.currentSelected = selection
    },
    /**
     * 选择每页显示数量
     */
    handleSizeChange(val) {
      this.pagination.pagesize = val
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    /**
     * 选择当页面
     */
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.loadTableData()
    },

    /**
     *选择组织
     */
    handleSelectOrganizeChange: function() {
      this.editFrom.OrganizeId = this.selectedOrganizeOptions
    },
    dateformatter(row, column, cellValue, index) {
      var date = row[column.property]
      return cellValue ? fecha.format(new Date(date), 'yyyy-MM-dd') : ''
    },
    handleResetPassword: function(val) {
      if (this.currentSelected.length > 1 || this.currentSelected.length === 0) {
        this.$alert('请先选择要操作的数据', '提示')
        return false
      } else {
        const data = {
          userId: this.currentSelected[0].Id
        }
        resetPassword(data).then(res => {
          if (res.Success) {
            this.$alert('重置密码成功，新密为' + res.ErrMsg, '提醒', {
              confirmButtonText: '确定',
              callback: action => {
              }
            })
            this.currentSelected = ''
            this.loadTableData()
          } else {
            this.$message({
              message: res.ErrMsg,
              type: 'error'
            })
          }
        })
      }
    },
    SaveReferralEditForm() {
      this.$refs['editUserReferralFrom'].validate((valid) => {
        if (valid) {
          const data = {
            ReferralUserId: this.editUserReferralFrom.ReferralUserId,
            Id: this.currentId
          }
          UpdateReferral(data).then(res => {
            if (res.Success) {
              this.$message({
                message: '恭喜你，操作成功',
                type: 'success'
              })
              this.$refs['editUserReferralFrom'].resetFields()
              this.dialogReferralEditFormVisible = false
              this.loadTableData()
            } else {
              this.$message({
                message: res.ErrMsg,
                type: 'error'
              })
            }
          })
        } else {
          return false
        }
      })
    },
    SaveLevelEditForm() {
      this.$refs['editUserLevelFrom'].validate((valid) => {
        if (valid) {
          const data = {
            MemberGradeId: this.editUserLevelFrom.MemberGradeId,
            Id: this.currentId
          }
          UpdateLevel(data).then(res => {
            if (res.Success) {
              this.$message({
                message: '恭喜你，操作成功',
                type: 'success'
              })
              this.$refs['editUserLevelFrom'].resetFields()
              this.dialogLevleEditFormVisible = false
              this.loadTableData()
            } else {
              this.$message({
                message: res.ErrMsg,
                type: 'error'
              })
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
